﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Data.Linq;
using ModelLayer;

namespace DAL
{
    public class UserDB
    {
        public void CreateUser(string fname, string lname, string uname, string email, string pass, string salt)
        {
            using (var db = new BalderDataContext())
            {
                var u = new User();
                var c = new Credential();

                u.FName = fname;
                u.LName = lname;
                u.UserName = uname;
                u.Email = email;


                db.Users.InsertOnSubmit(u);
                db.SubmitChanges();
                c.UserID = u.UserId;
                c.Password = pass;
                c.Salt = salt;

                Debug.WriteLine("Salt: " +salt+ " Pass: " +pass);
                Debug.Write("userId" + c.UserID);

                db.Credentials.InsertOnSubmit(c);
               
                db.SubmitChanges();
            }
        }
        public ModelLayer.User GetUserById(int id)
        {
         using (var db = new BalderDataContext())
         {
             
             var p = db.Users.Single(x => x.UserId == id);
             var u = new ModelLayer.User(p.FName, p.LName, p.UserName, p.Email);
             return u;
         }
        }

        public ModelLayer.User GetUserByEmail(string email)
        {
            using (var db = new BalderDataContext())
            {
                var p = db.Users.Single(x => x.Email == email);
                var u = new ModelLayer.User(p.FName, p.LName, p.UserName, p.Email);
                return u;
            }
        }

        public void AddFriend(int uid, int fid)
        {
            using (var db = new BalderDataContext())
            {
                var u = db.Users.Single(x => x.UserId == uid);
                var friend = db.Users.Single(x => x.UserId == fid);
                var uf = new UserFriend();
                uf.UserId = u.UserId;
                uf.FriendId = friend.UserId;

                db.UserFriends.InsertOnSubmit(uf);

                db.SubmitChanges();
            }
        }

        public List<ModelLayer.User> GetFriends(string uname)
        {
            var users = new List<ModelLayer.User>();
            var u = new ModelLayer.User();
            using (var db = new BalderDataContext())
            {
                var user = db.Users.Single(x => x.UserName == uname);
                var friends = db.UserFriends.Where(o => o.User == user)
                                .Select(o => o.User1);
                foreach (var friend in friends)
                {
                    u.UserId = friend.UserId;
                    u.FName = friend.FName;
                    u.LName = friend.LName;
                    u.Email = friend.Email;
                    users.Add(u);
                }
                return users;
            }
        }

        public ModelLayer.User SearchUName(string uname)
        {
            var user = new ModelLayer.User();
            using (var db = new BalderDataContext())
            {
                var u = db.Users.Single(x => x.UserName == uname);
                user.FName = u.FName;
                user.LName = u.LName;
                user.UserId = u.UserId;
                user.UserName = u.UserName;
                user.Email = u.Email;
            }
            return user;
        }

        public List<ModelLayer.User> SearchUsersWithUsername(string searchString)
        {
            List<ModelLayer.User> users = new List<ModelLayer.User>();
            using (var db = new BalderDataContext())
            {

                var usersFromDB = (from u in db.Users
                                   where u.UserName.Contains(searchString)
                                   select u).Take(20);

                foreach (var userFromDB in usersFromDB)
                {

                    ModelLayer.User user = new ModelLayer.User();
                    user.UserId = userFromDB.UserId;
                    user.UserName = userFromDB.UserName;
                    users.Add(user);
                }
            }
        

                return users;
        }

        public List<ModelLayer.User> SearchUsersWithEmail(string searchString)
        {
            List<ModelLayer.User> users = new List<ModelLayer.User>();
            using (var db = new BalderDataContext())
            {

                var userFromDB = (from u in db.Users
                                   where u.Email.Contains(searchString)
                                   select u).First();

                
                    ModelLayer.User user = new ModelLayer.User();
                    user.UserId = userFromDB.UserId;
                    user.UserName = userFromDB.UserName;     
                    users.Add(user);
            }


            return users;
        }

        public Credential Login(int uid)
        {
            var cred = new Credential();
            using (var db = new BalderDataContext())
            {
                var user = new ModelLayer.User();
                var u = db.Users.Single(x => x.UserId == uid);
                var c = db.Credentials.Single(x => x.UserID == u.UserId);
                cred.UserID = c.UserID;
                cred.Password = c.Password;
                cred.Salt = c.Salt;
            }
            return cred;
        }

        public void AddToBlacklist(int userId, int blacklistId)
        {
            using (var db = new BalderDataContext())
            {
                var bList = new Blacklist {UserId = userId, BlaklistUserId = blacklistId, Active = true};
                db.Blacklists.InsertOnSubmit(bList);
                db.SubmitChanges();
            }
        }

        public void RemoveFromBlacklist(int userId, int blacklistId)
        {
            using (var db = new BalderDataContext())
            {
                var selectRow = db.Blacklists.Single(x => x.UserId == userId && x.BlaklistUserId == blacklistId);
                selectRow.Active = false;
                db.SubmitChanges();
            }
        }
    }
}
